package network.udp.udp_send_all;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-26 16:40
 **/
public class SendAll {
    public static void main(String[] args) {
        send();
    }

    private static void send() {
        DatagramSocket ds = null;
        try {
            //建立连接
            ds = new DatagramSocket();

            //准备数据
            byte[] bytes = "广播消息".getBytes();
            //通过广播地址发出去就行了
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length,
                    InetAddress.getByName("255.255.255.255"), 10000);

            //发送数据
            ds.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭io
            if (ds != null) {
                ds.close();
            }
        }
    }
}
